package com.ft.admix.gdt.adapter;

import android.app.Activity;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.bytedance.sdk.openadsdk.mediation.ad.MediationAdSlot;
import com.ft.admix.ad.MixCustomNativeAdapter;
import com.ft.admix.common.BiddingType;
import com.ft.admix.config.AdConfig;
import com.ft.admix.utils.ThreadUtils;

import java.util.ArrayList;
import java.util.List;

public class NativeAdapterLoader extends MixCustomNativeAdapter {

    @Override
    public void load(Activity context, AdConfig config) {
        ThreadUtils.runOnThreadPool(() -> {
                    TTAdNative adNativeLoader = TTAdSdk.getAdManager().createAdNative(context);
                    AdSlot adslot = new AdSlot.Builder()
                            .setCodeId(config.getAdId())
                            .setOrientation(TTAdConstant.VERTICAL)//横竖屏设置
                            .setMediationAdSlot(new MediationAdSlot
                                    .Builder()
                                    .setAllowShowCloseBtn(true)
                                    .build())
                            .build();
                    adNativeLoader.loadNativeExpressAd(adslot, new TTAdNative.NativeExpressAdListener() {
                        @Override
                        public void onError(int i, String s) {
                            callLoadFailed(i, s);
                        }

                        @Override
                        public void onNativeExpressAdLoad(List<TTNativeExpressAd> list) {
                            if (list == null || list.isEmpty()) {
                                callLoadFailed(999, "无广告数据");
                                return;
                            }
                            List<CustomExpressNativeAd> nativeAds = new ArrayList<>();
                            for (TTNativeExpressAd ttNativeExpressAd : list) {
                                nativeAds.add(new CustomExpressNativeAd(ttNativeExpressAd, BiddingType.TYPE_CLIENT));
                            }
                            callLoadSuccess(nativeAds);
                        }
                    });
                }
        );
    }

    @Override
    public void destroy() {

    }
}
